package com.qt.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/9/16 20:20
 */
@Slf4j
@Component
public class UserRealm  extends AuthorizingRealm {

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
         log.info("授权: {}" , principalCollection);
        return null;
    }

    /**
     * 认证
     * @param
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        log.info("认证: {}" , token);
        //用户名 ，密码  数据中取
        String name="pgw";
        String password="123456";

        //创建令牌
        UsernamePasswordToken  usertoken = (UsernamePasswordToken) token;
        //判断传入的数据
        if (!usertoken.getUsername().equals(name)){
            return null;//抛出异常 UnknownAccountException
        }
        //密码认证，shiro做
        return new SimpleAuthenticationInfo("",password,"");


    }
}
